﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Finance.Core.Provider;
using Finance.Core.Provider.XmlProvider;
using System.Collections.Specialized;
using Finance.Core.Configuration;

namespace Finance.Core
{
    public static class FinanceService
    {
        #region Constants and Fields
        /// <summary>
        /// The lock object.
        /// </summary>
        private static readonly object lockObj = new object();
        
        #endregion

        #region Properties
            private static FinanceProvider _provider;
        /// <summary>
        ///     Gets the current provider.
        /// </summary>
        public static FinanceProvider Provider
        {
            get
            {
                LoadProvider();
                return _provider;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Load the providers.
        /// </summary>
        private static void LoadProvider()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                lock (lockObj)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        _provider = new XmlProvider();
                    }
                }
            }
        }

        #region Picture

        public static void Insert(FinanceItem item)
        {
            Provider.Insert(item);
        }

        public static void Delete(FinanceItem item)
        {
            Provider.Delete(item);
        }

        public static void Update(FinanceItem item)
        {
            Provider.Update(item);
        }

        public static List<FinanceItem> GetAllFinance()
        {
            return Provider.GetAllFinance();
        }

        public static FinanceItem GetFinanceItem(String id)
        {
            return Provider.GetFinanceItem(id);
        }

        public static void SaveAllFinance()
        {
            Provider.SaveAllFinance();
        }
        #endregion

        #region Settings
        public static StringDictionary LoadSettings()
        {
            return Provider.LoadSettings();
        }
        public static void SaveSettings(StringDictionary settings)
        {
            Provider.SaveSettings(settings);
        }

        public static string GetStorageLocation()
        {
            return Provider.StorageLocation();
        }
        #endregion

        #region Card
        public static void Insert(CardItem item)
        {
            Provider.Insert(item);
        }

        public static void Delete(CardItem item)
        {
            Provider.Delete(item);
        }

        public static void Update(CardItem item)
        {
            Provider.Update(item);
        }

        public static List<CardItem> GetAllCard()
        {
            return Provider.GetAllCard();
        }

        public static CardItem GetCardItem(String id)
        {
            return Provider.GetCardItem(id);
        }

        public static void SaveAllCard()
        {
            Provider.SaveAllCard();
        }
        #endregion
        #endregion


    }
}
